<?php
namespace admin\model;

use admin\model\Model;

class TypeModel extends Model{
    
    /**
     * 查询类别的数量
     */
    public function getTypeCount($params){
        $sql = 'SELECT COUNT(*) AS count FROM blog_type';
        if(isset($params['name'])){
            $sql.=' WHERE name LIKE :name';
            $data = $this->p_query($sql,array('name'=>'%'.$params['name'].'%'));
        }else{
            $data = $this->query($sql);
        }
        return $data;
    }
    /**
     * 查询所有类别
     */
    public function getTypeAll($params = array('page' => 0,'limit' => 0)){
        $curr = ($params['page']-1)*$params['limit'];
        if($params['state'] == 'normal'){
            $sql = "SELECT blog_type.id,blog_type.name,blog_type.state FROM blog_type ORDER BY blog_type.id ASC";     //查找所有
            $data = $this->queryAll($sql);
        }else{
            $sql = "SELECT blog_type.id,blog_type.name,blog_type.state FROM blog_type ";     //查找所有
            if(isset($params['name'])){
                $sql.=" WHERE name LIKE :name ORDER BY blog_type.id ASC LIMIT {$curr},{$params['limit']} ";
                // echo $sql;
                $data = $this->p_queryAll($sql,array('name'=>'%'.$params['name'].'%'));
            }else{
                $sql.=" ORDER BY blog_type.id ASC LIMIT {$curr},{$params['limit']} ";
                $data = $this->queryAll($sql);
            }
        }
        return $this->filter($data);
    }
    /**
     * 过滤器
     */
    public function filter($data){
        $len = count($data,COUNT_NORMAL);       //统计数组长度
        for($i=0;$i<$len;++$i){
            $data[$i]['state'] = $data[$i]['state']==='0'?'禁用':'正常';
        }
        return $data;
    }
    /**
     * 设置类别状态为禁用
     */
    public function banType($params){
        $sql = 'UPDATE blog_type SET state = 0 WHERE id =:id';
        $this->p_execute($sql,$params);
        $data = [];
        if($this->db->num == 0){
            $data['code'] = '0';
        }else{
            $data['code'] = '1';
        }
        return $data;
    }

    /**
     * 更新类别的信息           
     */
    public function updateType($params){
        $check = $this->getTypeName(array('name'=>$params['name']));
        $data = [];
        if(count($check) != 0){
            $data['code'] = '2';            //类别名称已经存在
        }else{
            $sql = "UPDATE blog_type SET name = :name,state = :state WHERE id = :id";
            $this->p_execute($sql,$params);
            if($this->db->num == 0){
                $data['code'] = '0';
            }else{
                $data['code'] = '1';
            }
        }
        return $data;
    }
    /**
     * 添加类别信息
     */
    public function insertType($params){
        $check = $this->getTypeName(array('name'=>$params['name']));
        $data = [];
        if(count($check) != 0){
            $data['code'] = '2';            //类别名称已经存在
        }else{
            $sql = "INSERT blog_type(blog_type.name,blog_type.state) VALUES(:name,:state)";
            $this->p_execute($sql,$params);
            $insertId = $this->db->insertId;
            //判断是否插入成功，设置返回数据
            if($insertId !== null){
                $data['code'] = '1';        //成功
                $data['id'] = $insertId;
            }else{
                $data['code'] = '0';        //失败
                $data['id'] = $insertId;          
            }
        }
        return $data;
    }
    /**
     * 查询指定名称类型，检查是否存在
     */
    public function getTypeName($params){
        $sql = 'SELECT id FROM blog_type WHERE name = :name';
        return $this->p_queryAll($sql,$params);
    }
    /**
     * 删除指定Id的类别信息
     */
    public function delType($params){
        $sql1 = 'SELECT count(*) AS count FROM blog_info WHERE blog_type=:id';
        $check = $this->p_query($sql1,$params);
        $data = [];
        if($check['count'] == 0){
            $sql2 = 'DELETE FROM blog_type WHERE id = :id';
            $this->p_execute($sql2,$params);
            if($this->db->num == 0){
                $data['code'] = '0';        //错误
            }else{
                $data['code'] = '1';    //正常
            }
        }else{
            $data['code'] = '2';        //表示还有博客在用此类别，不能删除
        }
        return $data;
    }
}